$(document).ready(function(){
	var contextPath = document.location.pathname;
	var index = contextPath.substr(1).indexOf("/");
	contextPath = contextPath.substr(1, index);
	var basePath = "/" + contextPath;
	var radios = document.getElementsByName("isUse");
	
	var validObject = $("#addressInfor").Validform({
		
		dataType:'json',
		tiptype:2,
		ajaxPost:true,
		
		callback:function(data){
			 $("#addressBookInfor").modal('hide');
        	 var result = data.addressList;
        	 dynamicAddress(result);//动态生成地址列表
		},
		
		tiptype:function(msg,o,cssctl){
			
			if(!o.obj.is("form")){
				var tds=o.obj.text();
				
				var objtip=o.obj.siblings(".Validform_checktip");
				cssctl(objtip,o.type);
				objtip.text(msg);
			
				
			}	
			
		}
	
	});
	
	/**
	 * 初始化验证框架
	 */
	
	/**
	 * 订单下单事件
	 */
	$("#webOrderInfo").delegate('#orderSubmit', 'click', function() {
		var OpenPassword = $("#OpenPassword").val();
		if(OpenPassword!=""){
			if(OpenPassword.length == 3){
				
				$("#webOrderInfo").submit();
			}
			else{
				$("#OpenPassword").val("");
				$("div[id='passTip']").remove();
				$("#showOrhide").append("<div id='passTip' style='width: 100%;height: 40px;border: 1px solid red;'>礼品盒密码必须为3位数</div>");
			}
			
		}
		else{
			$("div[id='passTip']").remove();
			$("#showOrhide").append("<div id='passTip' style='width: 100%;height: 40px;border: 1px solid red;'>请设置礼品盒密码！</div>");
		}
	});
	
	getPostType();//初始化邮递类型
	
	/**
	 * 添加事件
	 */
	$("#publicMain").delegate('#addBtn', 'click', function() {
		initProvince();//加载省份
		
		$("#addressId").val("");
		$("#temp").val("");
		$("#address").val("");
		$("#postCode").val("");
		$("#receiver").val("");
		$("#mobile").val("");
		$("#telPhone").val("");
		
		
		$("#addressBookInfor").modal('show');
	});
	/**
	 * 保存/修改地址
	 */
	$("#optBtn").delegate('#saveBtn', 'click', function() {
		
		callValidform();//调用验证框架
	});
	
	/**
	 * 修改按钮事件
	 */

	$("#publicMain")
			.delegate(
					'#editBtn',
					'click',
					function() {
						initProvince();//初始化省份
						var memberId = $(this).attr("memberId");
						var addressId = $(this).attr("addressId");
						var editUrl = basePath+"/HRTWEB/address/edit.do";
						$.ajax({

							url : editUrl,
							type : "POST",
							dataType : 'json',
							data : {
								memberId : memberId,
								addressId:addressId
							},
							cache : false,

							success : function(data) {
								var address = data.address;
								loadAddress(address);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});

					});
	
	/**
	 * 设置默认地址
	 */
	$("#publicMain")
	.delegate('#setDefaultBtn','click',
			function() {
				var memberId = $(this).attr("memberId");
				var addressId = $(this).attr("addressId");
				alert("memberId---------"+memberId);
				alert("addressId---------"+addressId);
				var editUrl = basePath+"/HRTWEB/address/setDefaultAddress.do";
				$.ajax({
					
					url : editUrl,
					type : "POST",
					dataType : 'json',
					data : {
						memberId : memberId,
						addressId:addressId
					},
					cache : false,
					
					success : function(data) {
						var address = data.address;//返回默认地址
						/*alert(address.province);
						alert(address.postCode);
						alert(address.receiver);*/
						$("#receiver").attr("value",address.receiver);
						$("#postCode").attr("value",address.postCode);
						$("#province").attr("value",address.province);
						var tempAddress = address.province + address.city +address.area;
						$("#receAddr").val(tempAddress+address.address);
						var result = data.addressList;//返回地址列表
						dynamicAddress(result);
					},
					error : function(html) {
						alert("Load Data failly");
						refresh();
					}
				});
				
			});
	
	/**
	 * 模态窗口关闭时调用
	 */
	$("#addressBookInfor").on('hide.bs.modal', function() {
		clearAddress();
	});
	
	/*var divTotal = $("div[class='innerDIV']");
	for(var i = 0; i<divTotal.length;i++){
		divTotal[i].onclick= function(){
			 var addressDIV =  $(this).attr("id");
			 var addressId =  $(this).attr("objId");
			 alert("addressDIV-------->"+addressDIV);
			 alert("addressId-------->"+addressId);
			 var queryUrl =basePath+"/HRTWEB/address/getAddress.do?id="+addressId;
            	$.ajax({
					url : queryUrl,
					dataType : 'json',
					cache : false,
					success : function(data) {
						var address = data.addressBook;
						alert(address.province);
						alert(address.postCode);
						alert(address.receiver);
						$("#receiver").attr("value",address.receiver);
						$("#postCode").attr("value",address.postCode);
						$("#province").attr("value",address.province);
						var tempAddress = address.province + address.city +address.area;
						$("#currentAddress").text(tempAddress);
						$("#receAddr").val(tempAddress+address.address);
						$("#currentReceiver").text(address.receiver);
					},
					error : function(html) {
						alert("出错了");
						return;
					}
				});
				
			 $("#"+addressDIV).attr("isselected","selected");
			 $("#"+addressDIV).css("border","1px red solid");
			 
			 var divTotal = $("div[class='innerDIV']");
			 
			 var divObject = $("div[isselected='false']");
			 
			
			 
			 for(i=0;i<divObject.length;i++){
				var temp = $(divObject[i]).attr("id");
				$("#"+temp).css("border","");
			 }
			 
			 for(i=0;i<divTotal.length;i++){
					var temp = $(divTotal[i]).attr("id");
					$("#"+temp).attr("isselected","false");
					
			 }
			 
			}
	}*/
	
	/**
	 * 调用录音功能
	 */
	$("#publicMain")
	.delegate('#recordVideo','click',
			function() {
		alert("hello,world");
		getVideo();
		
	});
	
	/**
	 * 地址
	 */
	 $(".inner").delegate(
			  "div[class='innerDIV']",
			  "click",
			  function(){
				 var addressDIV =  $(this).attr("id");
				 var addressId =  $(this).attr("objId");
				 alert("addressDIV-------->"+addressDIV);
				 alert("addressId-------->"+addressId);
				 var queryUrl =basePath+"/HRTWEB/address/getAddress.do?id="+addressId;
	            	$.ajax({
						url : queryUrl,
						dataType : 'json',
						cache : false,
						success : function(data) {
							var address = data.addressBook;
							/*alert(address.province);
							alert(address.postCode);
							alert(address.receiver);*/
							$("#receiver").attr("value",address.receiver);
							$("#postCode").attr("value",address.postCode);
							$("#province").attr("value",address.province);
							var tempAddress = address.province + address.city +address.area;
							$("#currentAddress").text(tempAddress);
							$("#receAddr").val(tempAddress+address.address);
							$("#currentReceiver").text(address.receiver);
						},
						error : function(html) {
							alert("出错了");
							return;
						}
					});
					
				 $("#"+addressDIV).attr("isselected","selected");
				 $("#"+addressDIV).css("border","1px #963 solid");
				 $("#minPictrue"+addressId).attr("isselectedPic","selected");
				 $("#minPictrue"+addressId).css("visibility","visible");
				 
				 var divTotal = $("div[class='innerDIV']");
				 
				 var divObject = $("div[isselected='false']");
				 
				 var divMinPic = $("div[isselectedPic='false']");
				 
				 var divTotalMin = $("div[class='minPicture']");
				 
				 for(i=0;i<divObject.length;i++){
					var temp = $(divObject[i]).attr("id");
					$("#"+temp).css("border","");
					
				 }
				 
				 for(i=0;i<divTotal.length;i++){
						var temp = $(divTotal[i]).attr("id");
						$("#"+temp).attr("isselected","false");
						
				 }
				 
				 for(i=0;i<divTotalMin.length;i++){
						var tempMin = $(divTotalMin[i]).attr("id");
						$("#"+tempMin).attr("isselectedPic","false");
						
				 }
				 
				 for(i=0;i<divMinPic.length;i++){
						var tempMin = $(divMinPic[i]).attr("id");
						$("#"+tempMin).css("visibility","hidden");
						
					 }
				 
					/*$(".curmarker1").css("visibility","visible");
					$(".curmarker2").css("visibility","hidden");*/
			  });
			   
	
	
	 /**
	  * 初始化省份
	  */
	 function initProvince(){
		 var seachprov = $("#seachprov");
			var editUrl = basePath+"/HRTWEB/address/province/list.do";
			$.ajax({

				url : editUrl,
				dataType : 'json',
				cache : false,

				success : function(data) {
					var results = data.provinceList;
					
					var rowContent = "<option  value ='-1' >请选择</option>";
					if(results!=null){
						
						for(i=0;i<results.length;i++){
							var province =results[i];
							rowContent = rowContent+"<option  value ="+province.code+" >"+province.name+"</option>";
						}
					}
					seachprov.append(rowContent);
				},
				error : function(html) {
					alert("Load Data failly");
					refresh();
				}
			});
	 }
	 /**
	  * 初始化邮递类型
	  */
	 function getPostType(){
			var postUrl = basePath+"/web/orderInfor/post.do";
			$.ajax({

				url : postUrl,
				type : "POST",
				dataType : 'json',
				cache : false,

				success : function(data) {
					var results = data.postList;
					var commentStatus = $("#commentStatus");
					var rowContent = null;
					for(i=0;i<results.length;i++){
						var post = results[i];
						rowContent = rowContent+"<option  value ="+post.cost+" >"+post.name+"</option>";
					}
					commentStatus.append(rowContent);
				},
				error : function(html) {
					alert("Load Data failly");
					refresh();
				}
			});
		}
	 /**
	  * 验证方法
	  */
	 function callValidform(){
			
		 $("#addressInfor").attr("method", "post");

			var addressId = $("#addressId").val();
			alert("addressId---------->"+addressId);

			if (addressId == "") {
				$("#addressInfor").attr("action", ""+basePath+"/HRTWEB/address/save.do");
			} else {

				$("#addressInfor").attr("action", ""+basePath+"/HRTWEB/address/update.do");
			}
			
			//$("#addressInfor").Validform();
				
				
				
				
	}
});
function changePostType(cost){
	$("#postCost").text(cost);
	var summery = $("#tempSummery").text();
	var comCost = parseFloat(summery)+parseFloat(cost)
	$("#storeCost").text(comCost);
	$("#realCost").text(comCost);
	$("#summery").val(comCost);
	$("#mailFeed").val(cost);//邮费
}


function loadAddress(address) {
	$("#memberId").val(address.memberId);
	$("#addressId").val(address.id);
	$("#address").val(address.address);
	$("#postCode").val(address.postCode);
	$("#receiver").val(address.receiver);
	$("#mobile").val(address.mobile);
	$("#telPhone").val(address.telPhone);
	$("#addressBookInfor").modal('show');
}
function clearTable() {
	$("#addressBookTable tr:not(:first)").remove();
}
function clearAddress() {
	$("#seachprov").empty(); 
	$("#seachcity").empty(); 
	$("#seachdistrict").empty(); 
}
function refresh() {
	window.location.reload();
}




function dynamicAddress(result){
	 clearTable();
	 var addressBookTable = $("#addressBookTable");
	 
	 var rowContent = "<tr><td colspan='2'><div class='inner'><ul>";
	 for(i=0;i<result.length;i++){
		 var address = result[i];
		 rowContent = rowContent+"<li style='padding-top:15px;'>";
		 if(address.isdefault==0){
			 rowContent = rowContent+"<div class='innerDIV' id='address"+address.id+"' isselected='false' objId="+address.id+">";
		 }
		 else{
			 rowContent = rowContent+"<div class='innerDIV' id='address"+address.id+"' isselected='false' objId="+address.id+" style='border:1px red solid;'>";
			 
		 }
		 rowContent = rowContent+"<div><span>"+address.province+"</span><span>"+address.city+"</span><span>(</span><span>"+address.receiver+"收</span><span>)</span></div>";
		 rowContent = rowContent+"<div><span>"+address.area+"</span><span>"+address.address+"</span><span>"+address.mobile+"</span></div>";
		 rowContent = rowContent+ "<a href='#' id='editBtn' data-toggle='modal' memberId='"+address.memberId+"' addressId='"+address.id+"'>修改</a><a href='#' addressId='"+address.id+"' memberId='"+address.memberId+"' id='setDefaultBtn'>设为默认</a></div></li>";
		 
		 
	 }
	 rowContent = rowContent+"</ul></div></td></tr>";
	 
	 rowContent = rowContent+"<tr><td class='buyGift' colspan='2'>"
		+"<button type='button' id='addBtn' data-toggle='modal' memberId='${hrtMember.id}'>添加新地址</button></td></tr>";
	 addressBookTable.append(rowContent);
}



